﻿//
// GNS_PVEAllDateDaily.cpp
// MobileManage
//
// Created by MR.L on 16/2/18.
//
//
#include "GnsClientFrame.h"
#include "GNS_PVEAllDateDaily.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

//extern ISocketMgr * _GameSock;

GNS_PVEAllDateDaily::GNS_PVEAllDateDaily()
{
    pGNS_PVEAllDateDaily=nullptr;
    DataListView=nullptr;
    TimeText=nullptr;
    pCalendar=nullptr;
    pProName=nullptr;
    GameType=nullptr;
    GameName=nullptr;
    PlayerName=0;
    CurID=0;
    Year=0;
    Mon=0;
    Day=0;
}

GNS_PVEAllDateDaily::~GNS_PVEAllDateDaily()
{
    
}

GNS_PVEAllDateDaily* GNS_PVEAllDateDaily::createLayer()
{
    auto pGNS_PVEAllDateDaily = GNS_PVEAllDateDaily::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_PVEAllDateDaily);
    
    return pGNS_PVEAllDateDaily;
}

bool GNS_PVEAllDateDaily::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    /**
     *  @author MR.L, 16-02-16 11:02:50
     *
     *  获取当前时间
     */
    time_t tt;
    time(&tt);
    struct tm * time;
    time = localtime(&tt);
    
    Mon = (int)time->tm_mon+1;
    Year = time->tm_year+1900;
    Day  =(int)time->tm_mday;

	pPvEAllDatePlay = GNS_PvEAllDatePlay::createLayer();  //代理-推广 玩家数据分成
	pPvEAllDatePlay->setVisible(false);

    return true;
}
void GNS_PVEAllDateDaily::SetNodeName(std::string CsbName)
{
    pGNS_PVEAllDateDaily = CSLoader::createNode(CsbName);
    pGNS_PVEAllDateDaily->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_PVEAllDateDaily);
    addChild(pGNS_PVEAllDateDaily,999);
    
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_PVEAllDateDaily);
    
    auto pBackBtn = static_cast<Button*>(pGNS_PVEAllDateDaily->getChildByName("PvEAllDateBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PVEAllDateDaily::BtnCallBack, this));
    DataListView = static_cast<ListView*>(pGNS_PVEAllDateDaily->getChildByName("DateListView"));
    
    auto pTimeBtn = static_cast<Button*>(pGNS_PVEAllDateDaily->getChildByName("Button_3"));
    pTimeBtn->setTag(2);
	pTimeBtn->setVisible(false);
    //pTimeBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PVEAllDateDaily::BtnCallBack, this));
    
    auto pTimeSer = static_cast<Button*>(pGNS_PVEAllDateDaily->getChildByName("Button_8"));
    pTimeSer->setTag(2);
    //pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_PVEAllDateDaily::BtnCallBack, this));
    
    auto typrbtnSrt = static_cast<Button*>(pGNS_PVEAllDateDaily->getChildByName("Button_8_0"));
    typrbtnSrt->setTag(3);
    //typrbtnSrt->addTouchEventListener(CC_CALLBACK_2(GNS_PVEAllDateDaily::BtnCallBack, this));
    
    auto gamebtn1 = static_cast<Button*>(pGNS_PVEAllDateDaily->getChildByName("Button_3_0"));
    gamebtn1->setTag(3);
	gamebtn1->setVisible(false);
    //gamebtn1->addTouchEventListener(CC_CALLBACK_2(GNS_PVEAllDateDaily::BtnCallBack, this));
    
    TimeText = static_cast<Text*>(pGNS_PVEAllDateDaily->getChildByName("Text_3"));

	if ((int)GNS_PlayerMgr::getInstance()->getPlayProProFit()->size() > 0)
	{
		GNS_PlayerMgr::getInstance()->transferEachTime(Year, Mon, Day, GNS_PlayerMgr::getInstance()->getPlayProProFit()->at(0)._selDate);
	}
	TimeText->setString(StringUtils::format("%d-%02d-%02d", Year, Mon, Day));
    
    GameName=static_cast<Text*>(pGNS_PVEAllDateDaily->getChildByName("GameName"));
	if (GNS_PlayerMgr::getInstance()->getGameId() > 0)
	{
		GameName->setString(GNS_PlayerMgr::getInstance()->getGameIdInfo()[GNS_PlayerMgr::getInstance()->getGameId()]);
	}
    
    pProName = static_cast<Text*>(pGNS_PVEAllDateDaily->getChildByName("Text_4"));
    GameType = static_cast<ListView*>(pGNS_PVEAllDateDaily->getChildByName("GameType"));
    GameType->removeAllItems();
	int x = 0;
    for (auto it:(* GNS_PlayerMgr::getInstance()->getGameList())) {
        if (it.iGameType==1) {
            auto pbutton = Button::create("p0Btn.png");
			pbutton->setTitleText(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
            pbutton->setTitleColor(Color3B::BLACK);
            pbutton->setTitleFontSize(20);
            pbutton->setTag(7);
            pbutton->addTouchEventListener(CC_CALLBACK_2(GNS_PVEAllDateDaily::BtnCallBack, this));
            GameType->addChild(pbutton);

			++x;
			if (x == 1)
				GameName->setString(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
        }
        
    }
    GameType->setVisible(false);
    AcceptSeverDate();
}
void GNS_PVEAllDateDaily::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.erase(1);
                    this->removeAllChildrenWithCleanup(true);
					this->setVisible(false);
                    //this->getParent()->removeChild(this);
                }
                auto PromoterNode=GNS_PlayerMgr::getInstance()->_NodeChildVec.at(0);
                PromoterNode->setVisible(true);
                break;
            }
            case 2:
            {
					  /* pCalendar = GNS_Calendar::createCalendar();
					   pCalendar->SetCalendBgImage("Calendarbg.png");
					   std::vector<Button *> CalenderBtn;
					   pCalendar->GetTimeButton()->swap(CalenderBtn);
					   for (auto & it : CalenderBtn) {
					   it->setTag(100);
					   it->addTouchEventListener(CC_CALLBACK_2(GNS_PVEAllDateDaily::TimeButtonCallBack, this));
					   }*/
				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_PVEAllDateDaily::TimeButtonCallBack, this));
                break;
            }
            case 3:
            {
                if (GameType->isVisible()==true) {
                    GameType->setVisible(false);
                }else{
                    GameType->setVisible(true);
                }
                break;
            }
            case 7:
            {
                GameName->setString(pButton->getTitleText());
				auto st = SystemMgr::getInstance()->utf8ToGb(pButton->getTitleText().c_str());
				std::map<string, int>::iterator mapGameNameIter = GNS_PlayerMgr::getInstance()->getGameNameInfo().find(st);
				if (mapGameNameIter != GNS_PlayerMgr::getInstance()->getGameNameInfo().end())
				{
					GNS_PlayerMgr::getInstance()->getPlayProProFit()->clear();
					CMD_CS_GetManToMachPlayProfit pGetPlayPro;
					pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
					pGetPlayPro._uplineCode = CurID;
					std::string TimeStr = StringUtils::format("%d-%02d-%02d", Year, Mon, Day);
					memcpy(pGetPlayPro._selDate, TimeStr.c_str(), 11);
					pGetPlayPro._type = 2;
					pGetPlayPro._gameId = mapGameNameIter->second;
					GnsClientFrame::getInstance()->sendMsg(0, MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
					GameType->setVisible(false);
					GNS_PlayerMgr::getInstance()->setGameId(pGetPlayPro._gameId);
				}

                break;
            }
            default:
                break;
        }
    }
}

void GNS_PVEAllDateDaily::AcceptSeverDate()
{
    DataListView->removeAllItems();
    int index =(int)GNS_PlayerMgr::getInstance()->getPlayProProFit()->size();
    int x=0;
    Size bgSize;
    for (auto it:(*GNS_PlayerMgr::getInstance()->getPlayProProFit())) {
        auto Itembg = Button::create("line between1.png");
        DataListView->addChild(Itembg);
        Itembg->setTag(100+x);
        Itembg->addTouchEventListener(CC_CALLBACK_2(GNS_PVEAllDateDaily::ListViewBtn, this));
        Size bgSize = Itembg->getContentSize();
        
        auto Name = Text::create(StringUtils::format("%d",it._agentCode), GameFontName, 24);
        Name->setPosition(Vec2(bgSize.width*0.14, bgSize.height*0.34));
        Name->setColor(Color3B::BLACK);
        Itembg->addChild(Name);
        
        auto lineRevenueRatio = Text::create(StringUtils::format("%lld",it._gamePlayIn), GameFontName, 24);
        lineRevenueRatio->setPosition(Vec2(bgSize.width*0.35, bgSize.height*0.34));
        lineRevenueRatio->setColor(Color3B::BLACK);
        Itembg->addChild(lineRevenueRatio);
        
        auto agentRevenueRatio = Text::create(StringUtils::format("%lld",it._gamePlayOut), GameFontName, 24);
        agentRevenueRatio->setPosition(Vec2(bgSize.width*0.55, bgSize.height*0.34));
        agentRevenueRatio->setColor(Color3B::BLACK);
        Itembg->addChild(agentRevenueRatio);
        
        auto AgameProfit = Text::create(StringUtils::format("%lld",it._gameProfit), GameFontName, 24);
        AgameProfit->setPosition(Vec2(bgSize.width*0.76, bgSize.height*0.4));
        AgameProfit->setColor(Color3B::BLACK);
        Itembg->addChild(AgameProfit);
        x++;
    }
    if(bgSize.width!=0||bgSize.height!=0)
    {
        DataListView->setInnerContainerSize(Size(bgSize.width, (bgSize.height+10)*index));
    }

}
void GNS_PVEAllDateDaily::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        std::string BtnText = pButton->getTitleText();
        Mon = pCalendar->Mon;
        Year = pCalendar->year;
        Day = atoi(BtnText.c_str());
        TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,atoi(BtnText.c_str())));
        pCalendar->Show(false);
        
        GNS_PlayerMgr::getInstance()->getPlayProProFit()->clear();
        CMD_CS_GetManToMachPlayProfit pGetPlayPro;
		pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
		pGetPlayPro._uplineCode = CurID;
        std::string TimeStr = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
        memcpy(pGetPlayPro._selDate, TimeStr.c_str(), 11);
        pGetPlayPro._type=2;
		pGetPlayPro._gameId = GNS_PlayerMgr::getInstance()->getGameId();
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
    }
}
void GNS_PVEAllDateDaily::SelectedItemEvents(cocos2d::Ref *pSender, ui::ListView::EventType type)
{
    if (type==ui::ListView::EventType::ON_SELECTED_ITEM_START) {
        auto pListView = (ListView*)pSender;
        int Index = (int)pListView->getCurSelectedIndex();
        int x =0;
        for (auto it:(*GNS_PlayerMgr::getInstance()->getGameProReveRes())) {
            if (Index==x) {
                CMD_CS_GetManToMachPlayProfit pGetPlayPro;
				pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
				pGetPlayPro._uplineCode = it._lowlineCode;
                std::string TimeStr = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                memcpy(pGetPlayPro._selDate, TimeStr.c_str(), 11);
                pGetPlayPro._type=3;
				pGetPlayPro._gameId = GNS_PlayerMgr::getInstance()->getGameId();
                GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
                PlayerName=it._lowlineCode;
            }
            x++;
        }
    }
}
void GNS_PVEAllDateDaily::SetProName(std::string Name)
{
	if (pProName != nullptr)
	{
		std::string strAgentCode = CCGnsOften::getUtf8_DirectAssignmentInProgram("推广员账号:");
		strAgentCode.append(SystemMgr::getInstance()->GbToUTF8(Name.c_str()));
		pProName->setString(strAgentCode);
		pProName->setVisible(true);
	}
	CurID = atoi(Name.c_str());

    //pProName->setString(StringUtils::format("代理商账号:%s",Name.c_str()));
	//pProName->setVisible(true);
    //CurID=atoi(Name.c_str());
}

void GNS_PVEAllDateDaily::AccepPlayerDate()
{
	/*if (pGNS_PVEAllDateDaily != nullptr)
	{
		pGNS_PVEAllDateDaily->setVisible(false);
	}*/
    
	pPvEAllDatePlay->removeAllChildren();
	pPvEAllDatePlay->setVisible(true);
    pPvEAllDatePlay->SetNodeName("PvpPlayerDate.csb");   //从推广员进到 玩家
    pPvEAllDatePlay->SetProName(PlayerName,CurID);
	
	//this->setVisible(false);
}

void GNS_PVEAllDateDaily::ListViewBtn(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int Btntag = pButton->getTag();
        int x =0;
        for (auto it:(*GNS_PlayerMgr::getInstance()->getPlayProProFit())) {
            if (Btntag-100==x) {
                CMD_CS_GetManToMachPlayProfit pGetPlayPro;
                pGetPlayPro._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                pGetPlayPro._uplineCode=it._agentCode;
                std::string TimeStr = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                memcpy(pGetPlayPro._selDate, TimeStr.c_str(), 11);
                pGetPlayPro._type=3;
                pGetPlayPro._gameId=GNS_PlayerMgr::getInstance()->getGameId();
                GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
                PlayerName=it._agentCode;
                pGNS_PVEAllDateDaily->setVisible(false);
				break;
            }
            x++;
        }
        GNS_PlayerMgr::getInstance()->getPlayProPlayFit()->clear();
    }
}
